package com.netease.nie.service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.utils.CopyUtils;
import com.netease.nie.base.utils.CustomDateFormatUtils;
import com.netease.nie.common.CommonService;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dao.ExchangeGoodsInfoDao;
import com.netease.nie.dto.ExchangeGoodsInfoDto;
import com.netease.nie.dto.ExchangeGoodsInfoListDto;
import com.netease.nie.model.ExchangeGoodsInfo;

/**
 * 
 * @author Lin Zijie
 * @createDate Nov 17th,2016
 *
 */
@Service
public class ExchangeGoodsInfoServiceImpl extends CommonService implements ExchangeGoodsInfoService {

	@Autowired
	private ExchangeGoodsInfoDao exchangeGoodsInfoDao;

	public ExchangeGoodsInfoDto queryExchangeGoodsInfoById(int id) {
		ExchangeGoodsInfoDto exchangeGoodsInfoDto = new ExchangeGoodsInfoDto();
		ExchangeGoodsInfo exchangeGoodsInfo = exchangeGoodsInfoDao.queryGoodsInfoById(id);
		CopyUtils.copy(exchangeGoodsInfoDto, exchangeGoodsInfo);
		exchangeGoodsInfoDto.setPutOffTime(CustomDateFormatUtils.getyyyyMMddStr(exchangeGoodsInfo.getPutOffTime()));
		exchangeGoodsInfoDto.setPutOnTime(CustomDateFormatUtils.getyyyyMMddStr(exchangeGoodsInfo.getPutOnTime()));
		exchangeGoodsInfoDto.setValidDate(computeValidDate(exchangeGoodsInfo));
		exchangeGoodsInfoDto.setThumbnailUrl(exchangeGoodsInfo.getThumbnailWapUrl());
		return exchangeGoodsInfoDto;
	}
	
	private String computeValidDate(ExchangeGoodsInfo exchangeGoodsInfo) {
		if (exchangeGoodsInfo == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		Integer changeableValidDate = exchangeGoodsInfo.getChangeableValidDate();
		Integer fixedValidDate = exchangeGoodsInfo.getFixedValidDate();
		String validDate = StringUtils.EMPTY;
		if (changeableValidDate != null) {
			validDate = "兑换后" + changeableValidDate + "天有效";
		} else if (fixedValidDate != null) {			
			DateFormat df = new SimpleDateFormat("yyyyMMdd");
			Date parsedDate = null;
			try {
				parsedDate = df.parse(CustomDateFormatUtils
						.getyyyyMMddStr(fixedValidDate));
			} catch (ParseException e) {
				
			}
			validDate = CustomDateFormatUtils.getyyyyMMddStr(parsedDate.getTime());
		}
		
		return validDate;
	}

	public ExchangeGoodsInfoDto queryExchangeGoodsInfoByIdWithoutPutOnStatus(int id) {
		ExchangeGoodsInfoDto exchangeGoodsInfoDto = new ExchangeGoodsInfoDto();
		ExchangeGoodsInfo exchangeGoodsInfo = exchangeGoodsInfoDao.queryGoodsInfoByIdWithoutStatus(id);
		if (exchangeGoodsInfo == null)
			throw new BusinessException(BusinessConstants.GOODS_NOT_EXISTS.getCode(), "商品不存在");
		
/*		if (exchangeGoodsInfo.getIsDeleted() == DeletedFlagConstants.DELETED.getCode())
			throw new BusinessException(BusinessConstants.GOODS_HAD_DELETED.getCode(), "商品已删除");*/ //删除也要查看
		
		CopyUtils.copy(exchangeGoodsInfoDto, exchangeGoodsInfo);
		exchangeGoodsInfoDto.setValidDate(computeValidDate(exchangeGoodsInfo));
		
		return exchangeGoodsInfoDto;
	}
	
	public Map<Integer, ExchangeGoodsInfo> queryExchangeGoodsInfoByIds(List<Integer> ids) {
		if (ids.isEmpty())
			return Collections.emptyMap();
		
		List<ExchangeGoodsInfo> exchangeGoodsInfoList = exchangeGoodsInfoDao.queryGoodsInfoListByIds(ids);
		
		if (exchangeGoodsInfoList == null || exchangeGoodsInfoList.isEmpty())
			return Collections.emptyMap();
		
		Map<Integer, ExchangeGoodsInfo> exchangeGoodsInfoMap = new HashMap<>();
		
		int size = exchangeGoodsInfoList.size();
		int goodsId;
		ExchangeGoodsInfo exchangeGoodsInfo = null;
		
		for (int i = 0; i < size; ++i) {
			exchangeGoodsInfo = exchangeGoodsInfoList.get(i);
			goodsId = exchangeGoodsInfo.getId();
			exchangeGoodsInfoMap.put(goodsId, exchangeGoodsInfo);
		}
		
		return exchangeGoodsInfoMap;
	}

	public List<ExchangeGoodsInfoDto> queryExchangeGoodsInfoDtosByIds(
			List<Integer> ids) {
		if (ids.isEmpty())
			return Collections.emptyList();
		
		List<ExchangeGoodsInfo> exchangeGoodsInfoList = exchangeGoodsInfoDao.queryGoodsInfoListByIds(ids);
		if (exchangeGoodsInfoList != null && !exchangeGoodsInfoList.isEmpty()) {
			ExchangeGoodsInfo exchangeGoodsInfo = null;
			ExchangeGoodsInfoDto exchangeGoodsInfoDto = null;
			int exchangeGoodsInfoListSize = exchangeGoodsInfoList.size();
			List<ExchangeGoodsInfoDto> exchangeGoodsInfoDtoList = new ArrayList<ExchangeGoodsInfoDto>();
			for (int i = 0; i < exchangeGoodsInfoListSize; i++) {
				exchangeGoodsInfo = exchangeGoodsInfoList.get(i);
				exchangeGoodsInfoDto = new ExchangeGoodsInfoDto();
				CopyUtils.copy(exchangeGoodsInfoDto, exchangeGoodsInfo);
				exchangeGoodsInfoDtoList.add(exchangeGoodsInfoDto);
			}
			return exchangeGoodsInfoDtoList;
		}
		return Collections.emptyList();
	}

	public List<ExchangeGoodsInfoListDto> queryExchangeGoodsInfoListDtosByIds(
			List<Integer> ids) {
		if (ids.isEmpty())
			return Collections.emptyList();
		
		List<ExchangeGoodsInfo> exchangeGoodsInfoList = exchangeGoodsInfoDao.queryGoodsInfoListByIds(ids);
		if (exchangeGoodsInfoList != null && !exchangeGoodsInfoList.isEmpty()) {
			ExchangeGoodsInfo exchangeGoodsInfo = null;
			ExchangeGoodsInfoListDto exchangeGoodsInfoListDto = null;
			int exchangeGoodsInfoListSize = exchangeGoodsInfoList.size();
			List<ExchangeGoodsInfoListDto> exchangeGoodsInfoListDtoList = new ArrayList<ExchangeGoodsInfoListDto>();
			for (int i = 0; i < exchangeGoodsInfoListSize; i++) {
				exchangeGoodsInfo = exchangeGoodsInfoList.get(i);
				exchangeGoodsInfoListDto = new ExchangeGoodsInfoListDto();
				CopyUtils.copy(exchangeGoodsInfoListDto, exchangeGoodsInfo);
				exchangeGoodsInfoListDtoList.add(exchangeGoodsInfoListDto);
			}
			return exchangeGoodsInfoListDtoList;
		}
		return Collections.emptyList();
	}
}
